<?php
class stflickr_set_model extends stmodel_2_1{

    public function __construct($config) {
        parent::__construct ( $config );
        $this->setTableName($this->prefix.'st_flickr_set');
        $this->setPrimaryKey('id');
    }

    public function savestFlickrToolboxSet($datas, $user_id) {
        $dataInsert = array();
        $dataUpdate = array();
        $i=0;
        if(count($datas) > 0){
            foreach ($datas as $data) {
                $id = $this->getId($data['id']);
                if($id){
                    $dataUpdate[$i]['id'] = $id;
                    $dataUpdate[$i]['set_id'] = $data['id'];
                    $dataUpdate[$i]['user_id'] = $user_id;
                    $dataUpdate[$i]['series'] = $data['secret'].'|*|'.$data['server'].'|*|'.$data['farm'];
                    $dataUpdate[$i]['title'] = $data['title'];
                    $dataUpdate[$i]['primary'] = $data['primary'];
                    $dataUpdate[$i]['description'] = $data['description'];

                }else{
                    $dataInsert[$i]['id'] = 'NULL';
                    $dataInsert[$i]['set_id'] = $data['id'];
                    $dataInsert[$i]['user_id'] = $user_id;
                    $dataInsert[$i]['series'] = $data['secret'].'|*|'.$data['server'].'|*|'.$data['farm'];
                    $dataInsert[$i]['title'] = $data['title'];
                    $dataInsert[$i]['primary'] = $data['primary'];
                    $dataInsert[$i]['description'] = $data['description'];
                }
                $i++;
            }
        }
        $this->inserRecords($dataInsert) ;
        $this->updateRecords($dataUpdate);
    }

    public function inserRecords($data = '', $where = ''){
        if(count($data)>0){
            $query = "INSERT INTO $this->_tableName
                    ( `id`, `set_id`, `user_id`, `series`, `primary`, `title`, `description` )
                    VALUES
                    ";
            $query1 = array();
            foreach ($data as $value) {
                $query1[] = $this->prepare("(%d, %s, %d, %s, %s, %s, %s)",
                        $value['id'], $value['set_id'], $value['user_id'], $value['series'], $value['primary']
                        , $value['title'], $value['description']
                        );
            }
            $query1 = implode(', ', $query1);
            $query .= ' '.$query1;
//            echo $query.'<br>';
            return $this->query( $query );
        }else{
            return false;
        }
    }

    public function updateRecords($data = '', $where = ''){
        if(count($data)>0){
            foreach ($data as $value) {
                $where = array($this->_primaryKey => $value[$this->_primaryKey]);
                $this->update($this->_tableName, $value, $where);
            }
        }
        return false;
    }

    public function getId($id){
        $query = "SELECT id
        FROM $this->_tableName
        WHERE set_id like ('$id')
        ";
        $result = $this->get_var($query);
        return $result;
    }

    public function countRecords($args = ''){

        $result = $this->getRecords($args);
        return count($result);

    }

}